#!/bin/bash

# 默认构建版本
BUILD_TYPE="release"

# 固件前缀
FIRMWARE_PREFIX="WiFiBase"

# 版本号
VERSION="1.0.0"

# 获取当前日期和时间（格式：YYYYMMDDHHMMSS）
CURRENT_DATE=$(date +%Y%m%d%H%M%S)

# 配置文件源目录
CONFIG_SOURCE_DIR="build_config/package"

# 新增feeds配置文件源目录
CONFIG_FEEDS_SOURCE_DIR="build_config/feeds"

# 新增target配置文件源目录
CONFIG_TARGET_SOURCE_DIR="build_config/target"

# package配置文件目标目录
CONFIG_PACKAGE_TARGET_DIR="package"
# feeds配置文件目标目录
CONFIG_FEEDS_TARGET_DIR="feeds"
# target配置文件目标目录
CONFIG_TARGET_TARGET_DIR="target"

# 显示帮助信息
show_help() {
    echo "Usage: $0 [options]"
    echo "Options:"
    echo "  -r, --release    Build the release version (default)"
    echo "  -d, --debug      Build the development version"
    echo "  -h, --help       Show this help message and exit"
}

# 处理命令行参数
case "$1" in
    -r|--release)
        BUILD_TYPE="release"
        ;;
    -d|--debug)
        BUILD_TYPE="development"
        ;;
    -h|--help)
        show_help
        exit 0
        ;;
    *)
        echo "Unknown option: $1"
        show_help
        exit 1
        ;;
esac

# 生成固件名称
FIRMWARE_NAME="${FIRMWARE_PREFIX}_${VERSION}_${CURRENT_DATE}"

# 根据构建类型执行不同的操作
if [ "$BUILD_TYPE" == "release" ]; then
    echo "Building the release version..."
    echo "Firmware name: ${FIRMWARE_NAME}_release.bin"

    # 拷贝 release 配置文件
    find "$CONFIG_SOURCE_DIR" -type f -name "*.release" | while read -r file; do
        target_file=$(echo "$file" | sed "s|$CONFIG_SOURCE_DIR|$CONFIG_PACKAGE_TARGET_DIR|" | sed 's/\.release$//')
        cp "$file" "$target_file"
        echo "Copied: $file -> $target_file"
    done

    # 新增feeds配置文件处理（release分支）
    find "$CONFIG_FEEDS_SOURCE_DIR" -type f -name "*.release" | while read -r file; do
        target_file=$(echo "$file" | sed "s|$CONFIG_FEEDS_SOURCE_DIR|$CONFIG_FEEDS_TARGET_DIR|" | sed 's/\.release$//')
        cp "$file" "$target_file"
        echo "Copied: $file -> $target_file"
    done

    # 新增target配置文件处理（release分支）
    find "$CONFIG_TARGET_SOURCE_DIR" -type f -name "*.release" | while read -r file; do
        target_file=$(echo "$file" | sed "s|$CONFIG_TARGET_SOURCE_DIR|$CONFIG_TARGET_TARGET_DIR|" | sed 's/\.release$//')
        cp "$file" "$target_file"
        echo "Copied: $file -> $target_file"
    done

    # 在这里添加构建正式版本的命令
    # 例如：make -j$(nproc) V=s

elif [ "$BUILD_TYPE" == "development" ]; then
    echo "Building the development version..."
    echo "Firmware name: ${FIRMWARE_NAME}_debug.bin"

    # 拷贝 development 配置文件
    find "$CONFIG_SOURCE_DIR" -type f -name "*.development" | while read -r file; do
        target_file=$(echo "$file" | sed "s|$CONFIG_SOURCE_DIR|$CONFIG_PACKAGE_TARGET_DIR|" | sed 's/\.development$//')
        cp "$file" "$target_file"
        echo "Copied: $file -> $target_file"
    done

    # 新增feeds配置文件处理（development分支）
    find "$CONFIG_FEEDS_SOURCE_DIR" -type f -name "*.development" | while read -r file; do
        target_file=$(echo "$file" | sed "s|$CONFIG_FEEDS_SOURCE_DIR|$CONFIG_FEEDS_TARGET_DIR|" | sed 's/\.development$//')
        cp "$file" "$target_file"
        echo "Copied: $file -> $target_file"
    done

    # 新增target配置文件处理（development分支）
    find "$CONFIG_TARGET_SOURCE_DIR" -type f -name "*.development" | while read -r file; do
        target_file=$(echo "$file" | sed "s|$CONFIG_TARGET_SOURCE_DIR|$CONFIG_TARGET_TARGET_DIR|" | sed 's/\.development$//')
        cp "$file" "$target_file"
        echo "Copied: $file -> $target_file"
    done

    # 在这里添加构建开发版本的命令
    # 例如：make -j$(nproc) V=s CONFIG_DEBUG=y
fi

make -j 8

# 完成构建
echo "Build completed."
